package cn.shyouchi.wuguanjia.android.ui.attendance.report;

import android.graphics.BitmapFactory;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.attendance.AttendanceApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.controls.AttendanceResDialog;
import cn.shyouchi.wuguanjia.android.event.ReportSubmitEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.utils.DisplayUtil;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SubmitReportActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_report_time)
    TextView mTvReportTime;
    @BindView(R.id.tv_report_state)
    TextView mTvReportState;
    @BindView(R.id.tv_report_post)
    TextView mTvReportPost;
    @BindView(R.id.img_photo)
    ImageView mImgPhoto;
    //    @BindView(R.id.tv_report_content)
//    CheckBox mTvReportContent;
    @BindView(R.id.btn_submit)
    Button mBtnSubmit;


    String path;
    String postsettingid;

    StaffReportEntity.ResultMessageBean.PlanBean mPlanBean;
    StaffReportEntity.ResultMessageBean.DataBean mDataBean;

    AttendanceApi mAttendanceApi;
    @BindView(R.id.group_contents)
    LinearLayout mGroupContents;


    @Override
    public int initContentView() {
        return R.layout.activity_submit_report;
    }

    @Override
    public void initInjector() {
        path = getIntent().getStringExtra("photo");
        postsettingid = getIntent().getStringExtra("postsettingid");

        mPlanBean = getIntent().getParcelableExtra("reportinfo");
        mDataBean = getIntent().getParcelableExtra("datainfo");
        mAttendanceApi = new AttendanceApi(mContext);


    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        mImgPhoto.setImageBitmap(BitmapFactory.decodeFile(path));


        if (mPlanBean != null) {
            mTvReportTime.setText(mPlanBean.getTime());
        }

        if (mDataBean != null) {
            mTvReportState.setText("正常");
            mTvReportPost.setText(mDataBean.getPostsetting().getPostsetting_name());
//            mTvReportContent.setText(mDataBean.getPostsetting().getPostsetting_info());
        }

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        String[] strings = mDataBean.getPostsetting().getPostsetting_info().split("\n");
        for (String s : strings) {
            CheckBox checkBox = new CheckBox(mContext);
            checkBox.setText(s);
            checkBox.setButtonDrawable(R.drawable.selector_checkbox_check);
            checkBox.setLayoutParams(params);
            checkBox.setPadding(DisplayUtil.dip2px(mContext, 5), DisplayUtil.dip2px(mContext, 5), DisplayUtil.dip2px(mContext, 5), DisplayUtil.dip2px(mContext, 5));
            mGroupContents.addView(checkBox);
        }
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick(R.id.btn_submit)
    public void onClick() {

        if (checkTask()) {
            mAttendanceApi.reportAdd(postsettingid,
                    String.valueOf(mDataBean.getNewspaperpost_id()),
                    mPlanBean.getTime(),
                    String.valueOf(1),
                    path).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            AttendanceResDialog.newInstance("报岗结果", "提交失败！", String.valueOf(1)).show(getFragmentManager(), "signin");
                        }

                        @Override
                        public void onNext(BaseRequestResult result) {
                            EventBus.getDefault().post(new ReportSubmitEvent());
                            AttendanceResDialog dia = AttendanceResDialog.newInstance("报岗结果", "提交成功！", String.valueOf(0));
                            dia.setCancelable(false);
                            dia.setEnterClickListener(new AttendanceResDialog.OnEnterClickListener() {
                                @Override
                                public void click() {
                                    finish();
                                }
                            });
                            dia.show(getFragmentManager(), "signin");
                        }
                    });
        } else {
            ToastUtil.showToast("有任务还没确认，请勾选确认！");
        }
    }


    private boolean checkTask() {
        for (int i = 0; i < mGroupContents.getChildCount(); i++) {
            CheckBox cb = (CheckBox) mGroupContents.getChildAt(i);
            if (!cb.isChecked()) {
                return false;
            }
        }
        return true;
    }

}
